﻿using System.IO;
using Curriculum.DataSet;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress;

namespace Curriculum.Controls
{
    public partial class CandidateVacanciesXtraUserControl : XtraUserControl, IGridView, ICandidateList
    {
        private BackOfficeController _controller;

        #region Properties

        public BackOfficeController Controller
        {
            set
            {
                _controller = value;
                //SetGridDataSource();
                _controller.CandidateSaved += delegate { SetGridDataSource(); };
            }
        }

        #endregion

        public CandidateVacanciesXtraUserControl()
        {
            InitializeComponent();
            attachmentRepositoryItemButtonEdit.ButtonClick += AttachmentRepositoryItemButtonEditButtonClick;
        }

        public void Print()
        {
            var candidate = (CandidateVacancyDataSet.CandidateVacancyRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (candidate == null)
                return;
            //PrintManager printManager = new PrintManager();
            //var printPreviewXtraForm = new PrintPreviewXtraForm();
            //printPreviewXtraForm.Setup(candidateDataSet, "CandidateCrystalReport.rpt");
            //printPreviewXtraForm.ShowDialog();
        }

        public void Reload()
        {
            SetGridDataSource();
        }

        #region Private Methods

        public string GetExtendedFilter()
        {
            if (ReferenceEquals(gridView.ActiveFilterCriteria, null))
                return "";
            return gridView.ActiveFilterCriteria.LegacyToString();
        }


        private void SetGridDataSource()
        {
            var focusedRowHandle = gridView.FocusedRowHandle;
            var dataSet = _controller.GetCandidateVacancies(GetExtendedFilter());
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.CandidateVacancy.TableName;
            if (focusedRowHandle >= 0)
                gridView.FocusedRowHandle = focusedRowHandle;
        }

        #endregion

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "CandidateVacancies"; }
        }
        
        #endregion

        #region ICandidateList

        public long? CandidateId
        {
            get
            {
                var candidateVacancy = gridView.GetDataRow(gridView.FocusedRowHandle);
                if (candidateVacancy == null)
                    return null;
                return ((CandidateVacancyDataSet.CandidateVacancyRow)candidateVacancy).CandidateId;
            }
        }

        public long? VacancyId
        {
            get
            {
                var candidateVacancy = gridView.GetDataRow(gridView.FocusedRowHandle);
                if (candidateVacancy == null)
                    return null;
                return ((CandidateVacancyDataSet.CandidateVacancyRow)candidateVacancy).VacancyId;
            }
        }

        #endregion

        void AttachmentRepositoryItemButtonEditButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var candidate = (CandidateVacancyDataSet.CandidateVacancyRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (candidate.IsAttachmentTypeNull() || !candidate.HasAttachment)
                return;
            var tempFile = Path.GetTempPath() + "\\" + candidate.AttachmentName;
            var fileStream = new FileStream(tempFile, FileMode.Create);
            var attachment = _controller.GetCandidateAttachment(candidate.CandidateId);
            fileStream.Write(attachment, 0, attachment.Length);
            fileStream.Close();
            System.Diagnostics.Process.Start(tempFile);
        }
    }
}
